package com.dwxt.cdms.service;

import com.dwxt.cdms.base.Page;
import com.dwxt.cdms.dao.MedicalRecordImgMapper;
import com.dwxt.cdms.dao.MedicalRecordMapper;
import com.dwxt.cdms.entity.MedicalRecord;
import com.dwxt.cdms.entity.MedicalRecordImg;
import com.dwxt.cdms.entity.MedicalRecordImgExample;
import com.dwxt.cdms.entity.base.MediaRecordExt;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.util.JsonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class MediaRecordService {

    @Autowired
    private MedicalRecordMapper medicalRecordMapper;

    @Autowired
    private MedicalRecordImgMapper medicalRecordImgMapper;

    @Value("${gate.file.server}")
    private String server;

    @Value("${gate.file.realPath}")
    private String realPath;

    public BaseResult selectMediaRecordByCondition(MedicalRecord medicalRecord, String start, String end, Page page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<MedicalRecord> medicalRecords = medicalRecordMapper.selectMediaRecordByCondition(medicalRecord, start, end);
        PageInfo pageInfo = new PageInfo(medicalRecords);
        List<MediaRecordExt> list = new ArrayList<>();
        for (MedicalRecord record : medicalRecords) {
            MediaRecordExt recordExt = JsonUtils.jsonToPojo(JsonUtils.objectToJson(record), MediaRecordExt.class);
            List<MedicalRecordImg> medicalRecordImgs = selectMediaRecordImg(recordExt.getId());
            recordExt.setMedicalRecordImgList(medicalRecordImgs);
            list.add(recordExt);
        }
        pageInfo.setList(list);
        return BaseResult.ok(pageInfo);
    }

    @Transactional
    public BaseResult insert(MedicalRecord medicalRecord) {
        medicalRecordMapper.insertSelective(medicalRecord);
        MediaRecordExt mediaRecordExt = (MediaRecordExt) medicalRecord;
        List<MedicalRecordImg> medicalRecordImgList = mediaRecordExt.getMedicalRecordImgList();
        for (MedicalRecordImg medicalRecordImg : medicalRecordImgList) {
            medicalRecordImg.setMedicalRecordId(medicalRecord.getId());
            medicalRecordImgMapper.insertSelective(medicalRecordImg);
        }
        return BaseResult.ok();
    }

    public List<MedicalRecordImg> selectMediaRecordImg(Integer medicalRecordId) {
        MedicalRecordImgExample example = new MedicalRecordImgExample();
        example.createCriteria().andMedicalRecordIdEqualTo(medicalRecordId);
        List<MedicalRecordImg> medicalRecordImgs = medicalRecordImgMapper.selectByExample(example);
        return medicalRecordImgs;
    }

    @Transactional
    public BaseResult delete(Integer id) {
        //删除相关文件
        deleteMedicalRecordFile(id);
        //删除记录
        medicalRecordMapper.deleteByPrimaryKey(id);
        deleteImgByMedicalRecordId(id);
        return BaseResult.ok();
    }

    private void deleteMedicalRecordFile(Integer id) {
        try {
            MedicalRecord medicalRecord = medicalRecordMapper.selectByPrimaryKey(id);
            //删除文档文件
            String[] urls = medicalRecord.getDocUrl().split(",");
            for (String url : urls) {
                String filePath = realPath + url.substring(server.length());
                File file = new File(filePath);
                file.delete();
            }
            //删除图片文件
            List<MedicalRecordImg> medicalRecordImgs = selectMediaRecordImg(id);
            for (MedicalRecordImg medicalRecordImg : medicalRecordImgs) {
                String filePath = realPath + medicalRecordImg.getImgUrl().substring(server.length());
                File file = new File(filePath);
                file.delete();
            }
        } catch (Exception e) {
            log.error("删除文件失败：" + id, e);
        }
    }

    private void deleteImgByMedicalRecordId(Integer medicalRecordId) {
        MedicalRecordImgExample example = new MedicalRecordImgExample();
        example.createCriteria().andMedicalRecordIdEqualTo(medicalRecordId);
        medicalRecordImgMapper.deleteByExample(example);
    }

    public BaseResult update(MediaRecordExt mediaRecordExt) {
        //删除原有文件
        //deleteMedicalRecordFile(mediaRecordExt.getId());
        //修改记录
        medicalRecordMapper.updateByPrimaryKeySelective(mediaRecordExt);
        //删除原有的图片记录
        deleteImgByMedicalRecordId(mediaRecordExt.getId());
        for (MedicalRecordImg medicalRecordImg : mediaRecordExt.getMedicalRecordImgList()) {
            medicalRecordImg.setId(null);
            medicalRecordImg.setMedicalRecordId(mediaRecordExt.getId());
            medicalRecordImgMapper.insertSelective(medicalRecordImg);
        }
        return BaseResult.ok();
    }

    public BaseResult selectMediaById(Integer id) {
        MedicalRecord medicalRecord = medicalRecordMapper.selectByPrimaryKey(id);
        MedicalRecordImgExample example = new MedicalRecordImgExample();
        MedicalRecordImgExample.Criteria criteria = example.createCriteria();
        criteria.andMedicalRecordIdEqualTo(id);
        List<MedicalRecordImg> medicalRecordImgs = medicalRecordImgMapper.selectByExample(example);
        MediaRecordExt mediaRecordExt = new MediaRecordExt();
        BeanUtils.copyProperties(medicalRecord,mediaRecordExt);
        mediaRecordExt.setMedicalRecordImgList(medicalRecordImgs);
        return BaseResult.ok(mediaRecordExt);
    }
}
